package timing.ukulele.portal.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import timing.ukulele.persistence.mapper.BaseMapper;
import timing.ukulele.portal.persistent.SysTenant;
import timing.ukulele.portal.persistent.SysTenantUser;

import java.util.List;

@Mapper
@Repository
public interface SysTenantMapper extends BaseMapper<SysTenant> {

    /**
     * 租户用户
     *
     * @param tenantId 租户id
     * @param userId   用户id
     * @return list
     */
    @Select("<script>" +"select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where tenant_id=#{tenantId}" +
            " <if test=\"userId != null\"> AND user_id = #{userId} </if>" + "</script>")
    List<SysTenantUser> selectTenantUserList(@Param("tenantId") Long tenantId, @Param("userId") Long userId);

    /**
     * 租户用户
     *
     * @param page     分页对象
     * @param tenantId 租户id
     * @return list
     */
    @Select("select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where tenant_id=#{tenantId}")
    IPage<SysTenantUser> selectTenantUserPage(IPage<SysTenantUser> page, @Param("tenantId") Long tenantId);

    /**
     * 租户用户
     *
     * @param page     分页对象
     * @param tenantId 租户id
     * @param username 用户名
     * @return list
     */
    @Select("select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where tenant_id=#{tenantId} AND username_ like concat('%', #{username},'%')")
    IPage<SysTenantUser> selectTenantUserPageWithUserName(IPage<SysTenantUser> page, @Param("tenantId") Long tenantId, @Param("username") String username);

    /**
     * 根据用户名查询用户的租户
     *
     * @param username 用户名
     * @return list
     */
    @Select("select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where username_ = #{username}")
    List<SysTenantUser> selectUserTenantListByUsername(@Param("username") String username);

    /**
     * 根据用户名查询用户的租户
     *
     * @param page     分页对象
     * @param username 用户名
     * @return list
     */
    @Select("select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where username_ = #{username}")
    IPage<SysTenantUser> selectUserTenantPage(IPage<SysTenantUser> page, @Param("username") String username);

    /**
     * 根据用户id查询用户的租户
     *
     * @param userId 用户id
     * @return list
     */
    @Select("select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where user_id = #{userId}")
    List<SysTenantUser> selectUserTenantListByUserId(@Param("userId") Long userId);

    /**
     * 根据用户id查询用户的租户
     *
     * @param page   分页对象
     * @param userId 用户id
     * @return list
     */
    @Select("select user_id as userId,username_ as username,tenant_id as tenantId from sys_tenant_user where user_id = #{userId}")
    IPage<SysTenantUser> selectUserTenantPage(IPage<SysTenantUser> page, @Param("userId") Long userId);

    @Select("select count(*) from sys_tenant_user where tenant_id=#{tenantId}" +
            " <if test=\"username != null and username !=''\"> AND username_ = #{username} </if>" +
            " <if test=\"userId != null\"> AND user_id = #{userId} </if>")
    Integer checkUserTenant(@Param("tenantId") Long tenantId, @Param("userId") Long userId, @Param("username") String username);

    /**
     * 删除用户租户
     *
     * @param userId   用户id
     * @param tenantId 租户id
     */
    @Delete("delete from sys_tenant_user where user_id = #{userId} and tenant_id=#{tenantId}")
    void deleteTenantUser(@Param("userId") Long userId, @Param("tenantId") Long tenantId);

    /**
     * 新增用户租户
     *
     * @param tenantUser 用户租户对象
     */
    @Insert("<script>" + "insert into sys_tenant_user(tenant_id,user_id,username_) value(#{tenantUser.tenantId},#{tenantUser.userId},#{tenantUser.username})" + "</script>")
    void addTenantUser(@Param("tenantUser") SysTenantUser tenantUser);
}
